﻿using Module_Debug.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.VDDIC_Gamma
{
    public partial class RM690B0_QSPI_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();
        //Class_JOJO_gamma Flexible = new Class_JOJO_gamma();
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private static bool Gamma_adjust_timeout_flag = false;
        private int Total_points;
        //功耗 IRC USE
        public double CASDK_LUM = 0.0, CASDK_x = 0.0, CASDK_y = 0.0, CASDK_JEITA_Flicker = 0.0;
        float ELVDD = 0, ELVSS = 0, ELVSSC = 0, VCI = 0, VCIC = 0, VDDIO = 0, VDDIOC = 0, AVDD = 0, AVDDC = 0;
        public float ELVDDC = 0;
        float VBAT = 0, VBATC = 0, DVDD = 0, DVDDC = 0;

        Byte IRC_WEIGHT_R = 0, IRC_WEIGHT_G = 0, IRC_WEIGHT_B = 0;
        double Ir, Ig, Ib;

        //IRC Gamma
        decimal[] RR = new decimal[10];
        decimal[] GG = new decimal[10];
        decimal[] BB = new decimal[10];

        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();

        //RM690B0_QSPI 监听挂载
        frmMain frmHelp = null;

        public RM690B0_QSPI_Gamma(frmMain RM690B0_QSPI)
        {
            frmHelp = RM690B0_QSPI;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体   
        }
        private void RM690B0_QSPI_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM690B0_QSPI_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.Child_window_hide_already = false;
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM690B0_QSPI_Gamma_FormClosed！\r\n";
            frmHelp.IC_comboBox.Enabled = true;
        }
        private void RM690B0_QSPI_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM690B0_QSPI_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM690B0_QSPI_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(RM690B0_QSPI_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM690B0_QSPI_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
                Rbt_RM690B0_QSPI_P15_CheckedChanged(null, null);
            }
        }
        private void RM690B0_QSPI_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x70: RM690B0_QSPI的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if ((buffer[0] == 0x7D || buffer[0] == 0x9E || buffer[0] == 0x99) && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
                        List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
                        List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };

                        frmHelp.T1.Enabled = false; //停止超时计数
                        string str = "";                                               //华丽的分割线------------------------------
                        for (int b = 0; b < Setting.TempRecord.cmdLen; b++)
                        {
                            str = str + Convert.ToString(Rec_tmp[b]) + ",";

                        }
                        System.Diagnostics.Debug.Print(str);
                        //---------------------------------------------------RM690B0_QSPI---------------------------------------
                        if (Rec_tmp[0] == 0x7D)      //RM690B0_QSPI
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {

                                        string tmp = "";
                                        for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                        RM690B0_QSPI_txtRegData.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";

                                    }
                                    break;
                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x07:
                                    {
                                    }
                                    break;
                                case 0x04:
                                    {
                                        if ((Rec_tmp[3] & 0x10) == 0x10)    //27p
                                        {
                                            if ((Rec_tmp[3] & 0x0f) == 0x01)
                                            {
                                                for (int a = 0; a < 27; a++)
                                                {
                                                    R_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                            }
                                            else if ((Rec_tmp[3] & 0x0f) == 0x02)
                                            {
                                                for (int a = 0; a < 27; a++)
                                                {
                                                    G_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                            }
                                            else if ((Rec_tmp[3] & 0x0f) == 0x03)
                                            {
                                                for (int a = 0; a < 27; a++)
                                                {
                                                    B_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                            }

                                        }
                                        else
                                        {
                                            if ((Rec_tmp[3] & 0x0f) == 0x01)
                                            {
                                                for (int a = 0; a < 13; a++)
                                                {
                                                    R_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据_13P\t读 OK！\r\n";
                                            }
                                            else if ((Rec_tmp[3] & 0x0f) == 0x02)
                                            {
                                                for (int a = 0; a < 13; a++)
                                                {
                                                    G_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据_13P\t读 OK！\r\n";
                                            }
                                            else if ((Rec_tmp[3] & 0x0f) == 0x03)
                                            {
                                                for (int a = 0; a < 13; a++)
                                                {
                                                    B_Value[26 - a].Value = Decimal.Parse((Rec_tmp[5 + 2 * a] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6 + 2 * a].ToString());
                                                }
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据_13P\t读 OK！\r\n";
                                            }

                                        }
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t传输 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t传输 ERROR！\r\n";
                                    }
                                    break;

                                case 0x20:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x21:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash Finished \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash ERROR \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x22:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Flash IC Status: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x23:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x24:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x25:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x26:
                                    {
                                        string tmp = null;
                                        Setting.TempRecord.cmdLen = 14;
                                        for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + tmp + "\n ";

                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check_Flash 完成！\r\n";
                                    }
                                    break;
                                case 0x27:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Viewtrix Demura on  \t OK！\r\n";
                                    }
                                    break;
                                case 0x28:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Viewtrix Demura off  \t OK！\r\n";
                                    }
                                    break;
                                default: break;

                            }
                        }
                        else if (Rec_tmp[0] == 0x9E)
                        {
                            switch (Rec_tmp[3])
                            {
                                case 0x00:
                                    {
                                        T1.Enabled = false;
                                        ELVDDC = (float)(BitConverter.ToUInt16(Rec_tmp, 6) * 2.5) / 120;
                                        ELVDDC = ELVDDC - (float)(0.6112 - 0.007674 * ELVDDC);
                                        ELVSS = (float)(BitConverter.ToUInt16(Rec_tmp, 28) * 0.00157); if (ELVSS < 0.05) { ELVSS = 0; }
                                        //ELVSSC = (float)(474.6 - (BitConverter.ToUInt16(Rec_tmp, 22) * 0.152));
                                        ELVSSC = ELVDDC;
                                        if (ELVDDC != 0)
                                        {
                                            Random rd = new Random();
                                            int a = rd.Next(-55, 55);
                                            float f = (float)(a * 0.001);
                                            ELVSSC = ELVSSC + f;
                                        }

                                    }
                                    break;
                            }
                        }
                        else if (Rec_tmp[0] == 0x99) //GENWR
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x02:
                                    {
                                        string tmp = "";                    //8bit  read
                                        {
                                            for (int i = 3; i <= (Rec_tmp[2] + 2); i++)
                                            {
                                                if (i == (Rec_tmp[2] + 2))
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);//16进制
                                                }
                                                else if (i == 3)
                                                {
                                                    if (Rec_tmp[3] == 0xB0)
                                                    {

                                                    }
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                                }
                                                else
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                                }
                                            }
                                        }
                                        frmHelp.Generate_Read_Result_txt.AppendText(tmp + "\r\n");
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "visionox 8bit寄存器--读 OK！\r\n";
                                        Application.DoEvents();
                                    }
                                    break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        string[] P27_Grey = new string[] { "0", "1", "3", "7", "11", "15", "19", "23", "27", "31", "35", "39", "47", "55", "63", "79", "95", "111", "127", "143", "159", "175", "191", "207", "223", "239", "255" };
        string[] P13_Grey = new string[] { "0", " 1", " 3", " 7", " 11", " 15", "31", " 63", "95", " 127", "159", "191", "255" };
        //Gamma保存
        private void RM690B0_QSPI_SaveGamma_Click(object sender, EventArgs e)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };

            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    for (int i = 0; i < 27; i++)
                    {
                        string temp = P27_Grey[i].ToString();
                        str = str + "R" + temp + "," + R_Value[26 - i].Text + "," + "G" + temp + "," + G_Value[26 - i].Text + "," + "B" + temp + "," + B_Value[26 - i].Text + "\r\n";
                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
          //Gamma载入
        private void RM690B0_QSPI_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
                List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
                List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };
                for (int i = 0; i < 27; i++)
                {
                    R_Value[26 - i].Text = arrtmp[6 * i + 1].ToString();
                    G_Value[26 - i].Text = arrtmp[6 * i + 3].ToString();
                    B_Value[26 - i].Text = arrtmp[6 * i + 5].ToString();
                }
            }
        }
        public void RM690B0_QSPI_GAMMA_Click(object sender, EventArgs e)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };

            Button btnTmp = (Button)sender;
            if (rbt_RM690B0_QSPI_P27.Checked)
            {
                Send_buf = new byte[61];
            }
            else
            {
                Send_buf = new byte[33];
            }
            Send_buf[0] = 0x7D; //RM690B0_QSPI
            if (Gamma1.Checked)
            {
                Send_buf[4] = 0x01;
            }
            else if (Gamma2.Checked)
            {
                Send_buf[4] = 0x02;
            }
            else
            {
                Send_buf[4] = 0x03;
            }
            switch (btnTmp.Name)
            {
                case "RM690B0_QSPI_GammaR_Red":
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 56;//读Gamma
                            Setting.TempRecord.cmdLen = 61;
                            Send_buf[3] = 0x11;                            //红色red
                        }
                        else
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 28;//读Gamma
                            Send_buf[3] = 0x01;
                            Setting.TempRecord.cmdLen = 33;
                        }
                    }
                    break;
                case "RM690B0_QSPI_GammaR_Green":
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 56;//读Gamma
                            Send_buf[3] = 0x12;
                            Setting.TempRecord.cmdLen = 61;//红色red
                        }
                        else
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 28;//读Gamma
                            Send_buf[3] = 0x02;
                            Setting.TempRecord.cmdLen = 33;
                        }                                     /// Setting.TempRecord.RM690B0_QSPImdType = 0x04;
                    }
                    break;
                case "RM690B0_QSPI_GammaR_Blue":
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 56;//读Gamma
                            Send_buf[3] = 0x13;
                            Setting.TempRecord.cmdLen = 61;//红色red
                        }
                        else
                        {
                            Send_buf[1] = 0x04;
                            Send_buf[2] = 28;//读Gamma
                            Send_buf[3] = 0x03;
                            Setting.TempRecord.cmdLen = 33;
                        }
                    }
                    break;
                case "RM690B0_QSPI_GammaW_Red":
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 56;
                            Send_buf[3] = 0x11;
                            for (int i = 0; i < 27; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(R_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(R_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }
                        else
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 28;
                            Send_buf[3] = 0x01;
                            for (int i = 0; i < 13; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(R_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(R_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }

                    }
                    break;
                case "RM690B0_QSPI_GammaW_Green":
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 56;
                            Send_buf[3] = 0x12;
                            for (int i = 0; i < 27; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(G_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(G_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }
                        else
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 28;
                            Send_buf[3] = 0x02;
                            for (int i = 0; i < 13; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(G_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(G_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }

                    }
                    break;
                case "RM690B0_QSPI_GammaW_Blue":     //8
                    {
                        if (rbt_RM690B0_QSPI_P27.Checked)
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 56;
                            Send_buf[3] = 0x13;
                            for (int i = 0; i < 27; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(B_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(B_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }
                        else
                        {
                            Send_buf[1] = 0x03;
                            Send_buf[2] = 28;
                            Send_buf[3] = 0x03;
                            for (int i = 0; i < 13; i++)
                            {
                                Send_buf[2 * i + 5] = Convert.ToByte(Convert.ToUInt16(B_Value[26 - i].Value.ToString()) >> 8);
                                Send_buf[2 * i + 6] = Convert.ToByte(Convert.ToUInt16(B_Value[26 - i].Value.ToString()) & 0xff);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);   //RM690B0_QSPI
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }



        public void RM690B0_QSPI_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x70;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "VT_OTP": //otp Gamma
                    {
                        //Send_buf[1] = 0x0D;
                        //if (rbt_otp_p0.Checked == true)
                        //{ Send_buf[3] = 0x0F; }
                        //else if (rbt_otp_p1.Checked == true)
                        //{ Send_buf[3] = 0x1B; }
                        //else if (rbt_otp_p2.Checked == true)
                        //{ Send_buf[3] = 0x1C; }

                        //else if (rbt_otp_all.Checked == true)
                        //{ Send_buf[3] = 0x0E; }
                        //else if (rbt_otp_gamma.Checked == true)
                        //{ Send_buf[3] = 0x0D; }
                    }
                    break;
                //--------------------------------------------------------------------
                case "Read_Flash_ID":         //读Flash ID
                    {
                        Send_buf[1] = 0x20;
                    }
                    break;
                case "Erase_Flash":         //擦除Flash
                    {
                        Send_buf[1] = 0x21;
                        // frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 50 seconds\r\n";
                    }
                    break;
                case "Flash_Status":         //确认Flash Status
                    {
                        Send_buf[1] = 0x22;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "Check_Flash":         //确认Flash Status
                    {
                        Send_buf[1] = 0x23;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "Five_Step":         //确认Flash Status
                    {
                        Send_buf[1] = 0x24;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "OTP_Flash":         //确认Flash Status
                    {
                        Send_buf[1] = 0x25;
                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Please Wait for 40 seconds\r\n";
                    }
                    break;
                case "CRC_check":
                    {
                        Send_buf[1] = 0x26;
                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CRC_check\r\n";
                    }
                    break;
                case "Demura_on":
                    {
                        Send_buf[1] = 0x27;
                    }
                    break;
                case "Demura_off":
                    {
                        Send_buf[1] = 0x28;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }


        public void RM690B0_QSPI_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(RM690B0_QSPI_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "RM690B0_QSPI_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x7D;//RM690B0_QSPI
                        Send_buf[1] = 0x01;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "RM690B0_QSPI_RegR":
                    {
                        //  Setting.TempRecord.RM690B0_QSPImdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(RM690B0_QSPI_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x7D; //RM690B0_QSPI
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());  //cmd
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                default: break;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }


        public void btnAutoGammas_RM690B0_QSPI_Click(object sender, EventArgs e)
        {
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            frmHelp.numPatternR.Value = Diven_Offset[index].NO;
            frmHelp.numPatternG.Value = Diven_Offset[index].NO;
            frmHelp.numPatternB.Value = Diven_Offset[index].NO;
            Application.DoEvents();
            frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);

            Thread.Sleep(500);
            T20.Enabled = true;
            T20.Interval = 300;
        }
        public void btnVTStop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RM690B0_QSPI_auto_gamma_write(0x3637, 900, 0x6f70, 900, 0xa6a7, 0);
        }

        private void T20_Tick(object sender, EventArgs e)
        {
            if (rbt_RM690B0_QSPI_P13.Checked == true)
            {
                RM690B0_QSPI_Gamma_P13(null, null);
            }
            else if (rbt_RM690B0_QSPI_P27.Checked == true)
            {
                RM690B0_QSPI_Gamma_27P(null, null);
            }
        }

        private void BtnCalculateRM690B0_QSPI_Click(object sender, EventArgs e)
        {
            double L_X, L_Y, L_Z;
            string[] arr = RM690B0_QSPI_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }

            try  //获取调整spec
            {
                txtRM690B0_QSPI_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtRM690B0_QSPI_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtRM690B0_QSPI_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txtRM690B0_QSPI_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtRM690B0_QSPI_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtRM690B0_QSPI_x.Text = Diven_Gamma[0].X.ToString();
                txtRM690B0_QSPI_y.Text = Diven_Gamma[0].Y.ToString();

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        private void RM690B0_QSPI_auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[18];
            Setting.TempRecord.cmdLen = 17;

            Send_buf[0] = 0x7D;//RM692EX
                               //  if (radioButton_Page50.Checked == true)
            {
                Send_buf[1] = 0x06; //register normal gamma
            }          //{
                       //    Send_buf[1] = 0xDD; //register 
                       //}
            Send_buf[2] = 13; //length

            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            if (Gamma1.Checked)
            {
                Send_buf[15] = Convert.ToByte(0);
            }
            else if (Gamma2.Checked)
            {
                Send_buf[15] = Convert.ToByte(1);
            }
            else if (Gamma3.Checked)
            {
                Send_buf[15] = Convert.ToByte(2);
            }


            CRCData.CalculateCrc16(Send_buf, out Send_buf[16], out Send_buf[17]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 18);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }


        public void RM690B0_QSPI_Gamma_P13(Object myObject, EventArgs myEventArgs)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };

            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                frmHelp.Gamma_auto_adjust_count_set = 40;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM690B0_QSPI gamma调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                    {
                        Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                        Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                    }
                    //---------------------------------add-------------------------------------
                }
                if (index < Diven_Gamma[0].Points_num)
                {

                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数

                    Thread.Sleep(1000); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                frmHelp.Gamma_auto_adjust_count_set = 50;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;

                try
                {
                    switch (index)
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R_Value[index + 14].Value, G_Value[index + 14].Value, B_Value[index + 14].Value, Diven_Offset[index].NO, 0x3FF, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                if (index > 0)
                                {
                                    if (r > R_Value[14 + index - 1].Value)
                                    {
                                        r = R_Value[14 + index - 1].Value - 10;
                                    }
                                    if (g > G_Value[14 + index - 1].Value)
                                    {
                                        g = G_Value[14 + index - 1].Value - 10;
                                    }
                                    if (b > B_Value[14 + index - 1].Value)
                                    {
                                        b = B_Value[14 + index - 1].Value - 10;
                                    }
                                }
                            }
                            break;

                        case 11:
                            {
                                Gamma_adjust_timeout_flag = true;
                                r = (int)R_Value[14 - 1 + index].Value / 2;
                                g = (int)G_Value[14 - 1 + index].Value / 2;
                                b = (int)B_Value[14 - 1 + index].Value / 2;

                            }
                            break;
                        case 12:
                            {
                                Gamma_adjust_timeout_flag = true;
                                r = R_Value[14 + index].Value = 0;
                                g = G_Value[14 + index].Value = 0;
                                b = B_Value[14 + index].Value = 0;
                            }
                            break;
                        default:
                            break;
                    }
                    R_Value[14 + index].Value = r;
                    G_Value[14 + index].Value = g;
                    B_Value[14 + index].Value = b;
                    RM690B0_QSPI_auto_gamma_write(gamma_r[12 - index], (ushort)r, gamma_g[12 - index], (ushort)g, gamma_b[12 - index], (ushort)b);


                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }
        public void RM690B0_QSPI_Gamma_27P(Object myObject, EventArgs myEventArgs)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };

            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                frmHelp.Gamma_auto_adjust_count_set = 50;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM690B0_QSPI gamma调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                    {
                        Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                        Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                    }
                    //---------------------------------add-------------------------------------
                }
                if (index < Diven_Gamma[0].Points_num)
                {

                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数

                    Thread.Sleep(50); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                frmHelp.Gamma_auto_adjust_count_set = 50;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;

                try
                {
                    switch (index)
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                        case 13:
                        case 14:
                        case 15:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                        case 24:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R_Value[index].Value, G_Value[index].Value, B_Value[index].Value, Diven_Offset[index].NO, 0x3FF, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                if (index > 0)
                                {
                                    if (r > R_Value[index - 1].Value)
                                    {
                                        r = R_Value[index - 1].Value - 10;
                                    }
                                    if (g > G_Value[index - 1].Value)
                                    {
                                        g = G_Value[index - 1].Value - 10;
                                    }
                                    if (b > B_Value[index - 1].Value)
                                    {
                                        b = B_Value[index - 1].Value - 10;
                                    }
                                }
                                R_Value[index].Value = r;
                                G_Value[index].Value = g;
                                B_Value[index].Value = b;

                            }
                            break;

                        case 25:
                            {
                                Gamma_adjust_timeout_flag = true;
                                r = (int)R_Value[index - 1].Value / 2;
                                g = (int)G_Value[index - 1].Value / 2;
                                b = (int)B_Value[index - 1].Value / 2;

                            }
                            break;
                        case 26:
                            {
                                Gamma_adjust_timeout_flag = true;
                                r = R_Value[index].Value = 0;
                                g = G_Value[index].Value = 0;
                                b = B_Value[index].Value = 0;


                            }
                            break;
                        default:
                            break;
                    }

                    R_Value[index].Value = r;
                    G_Value[index].Value = g;
                    B_Value[index].Value = b;
                    RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Blue, null);
                    RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Green, null);
                    RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Red, null);
                    //RM690B0_QSPI_auto_gamma_write(gamma_r[26 - index], (ushort)r, gamma_g[26 - index], (ushort)g, gamma_b[26 - index], (ushort)b);
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");

                }
            }
        }



        //---------------------------------add-------------------------------------
        private int Multi_gamma_txt_now_index = 0;
        private int Multi_gamma_txt_next_index;
        private int Multi_gamma_txt_line_count = 0;
        private int Multi_gamma_group_index = 0;
        private void Btn_flexible_gamma_Click(object sender, EventArgs e)
        {
            Multi_gamma_txt_now_index = 0;
            Multi_gamma_txt_line_count = this.RM690B0_QSPI_Gamma_SetBox.Lines.GetUpperBound(0);
            Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
        }
        private void Multi_flexible_gammas(int txt_index, out int list_view_index)
        {
            string strLine = Convert.ToString(RM690B0_QSPI_Gamma_SetBox.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            string perline = string.Empty;
            string perline0 = string.Empty;
            list_view_index = 0;

            if (RM690B0_QSPI_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else  //标记关键事件
            {
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = txt_index; i < arr.Length;)  //获取的行数
                    {
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");

                        if (perline.Contains("REGW"))
                        {
                            perline = perline.Replace("REGW", "");
                            RM690B0_QSPI_txtRegData.Text = perline;
                            RM690B0_QSPI_RegWR_Click(RM690B0_QSPI_RegW, null);
                            Thread.Sleep(200);
                            i = i + 1;
                        }

                        else if (perline.Contains("SAVE"))
                        {
                            string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                            if (Directory.Exists(pathsave_file) == false)
                            {
                                Directory.CreateDirectory(pathsave_file);
                            }
                            string pathsave = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                            Multi_SaveGamma(pathsave);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_SET"))
                        {
                            perline = perline.Replace("GAMMA_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            System.Diagnostics.Debug.Print(Multi_gamma_group_index.ToString());             // 华丽的分割线----------------------

                            if (Multi_gamma_group_index == 0)
                            {
                                Gamma1.Checked = true;
                            }
                            else if (Multi_gamma_group_index == 1)
                            {
                                Gamma2.Checked = true;
                            }
                            else
                                Gamma3.Checked = true;

                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            for (int j = 0; j < gamma_set_lines.Length; j++)                        // 华丽的分割线----------------------
                            {
                                System.Diagnostics.Debug.Print(gamma_set_lines[j]);             // 华丽的分割线----------------------
                            }

                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();

                                RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Red, null);
                                Thread.Sleep(20);
                                RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Green, null);
                                Thread.Sleep(20);
                                RM690B0_QSPI_GAMMA_Click(RM690B0_QSPI_GammaW_Blue, null);
                                Thread.Sleep(20);
                            }
                            catch {; }
                            if (Total_points > 15)
                            {
                                rbt_RM690B0_QSPI_P27.Checked = true;
                            }
                            btnAutoGammas_RM690B0_QSPI_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }
                        else
                        {
                            i = i + 1;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("输入格式有误！");
                }
            }
        }
        private void Text_Processing(string[] txt_path)
        {
            double L_X, L_Y, L_Z;
            string[] arr = txt_path;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            try  //获取调整spec
            {
                txtRM690B0_QSPI_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtRM690B0_QSPI_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtRM690B0_QSPI_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txtRM690B0_QSPI_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtRM690B0_QSPI_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtRM690B0_QSPI_x.Text = Diven_Gamma[0].X.ToString();
                txtRM690B0_QSPI_y.Text = Diven_Gamma[0].Y.ToString();

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }

        //---------------------------------add-------------------------------------
        //Gamma自动保存
        private void Multi_SaveGamma(string path)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };
            try
            {
                string str = "保存Gamma文件";
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(path, false, Encoding.UTF8);
                for (int i = 0; i < 27; i++)
                {
                    string temp = P27_Grey[i].ToString();
                    str = str + "R" + temp + "," + R_Value[26 - i].Text + "," + "G" + temp + "," + G_Value[26 - i].Text + "," + "B" + temp + "," + B_Value[26 - i].Text + "\r\n";
                }
                sw.WriteLine(str);
                sw.Dispose(); //释放资源
                sw.Close(); //关闭数据流
            }
            catch
            {
                MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
            }
        } //

        //Gamma自动载入
        private void Multi_LoadGamma(string path)
        {
            string strtmp = "";
            //读取文本信息
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string strLine = streamReader.ReadLine();
            //循环读取文本信息
            while (!string.IsNullOrEmpty(strLine))
            {
                strtmp = strtmp + strLine + ",";
                strLine = streamReader.ReadLine(); //循环读取下一行
            }
            fs.Close(); //关闭流
            streamReader.Close();
            string[] arrtmp = strtmp.Split(new Char[] { ',' });
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13, RM690B_R14, RM690B_R15, RM690B_R16, RM690B_R17, RM690B_R18, RM690B_R19, RM690B_R20, RM690B_R21, RM690B_R22, RM690B_R23, RM690B_R24, RM690B_R25, RM690B_R26 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13, RM690B_G14, RM690B_G15, RM690B_G16, RM690B_G17, RM690B_G18, RM690B_G19, RM690B_G20, RM690B_G21, RM690B_G22, RM690B_G23, RM690B_G24, RM690B_G25, RM690B_G26 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13, RM690B_B14, RM690B_B15, RM690B_B16, RM690B_B17, RM690B_B18, RM690B_B19, RM690B_B20, RM690B_B21, RM690B_B22, RM690B_B23, RM690B_B24, RM690B_B25, RM690B_B26 };
            for (int i = 0; i < 27; i++)
            {
                R_Value[26 - i].Text = arrtmp[6 * i + 1].ToString();
                G_Value[26 - i].Text = arrtmp[6 * i + 3].ToString();
                B_Value[26 - i].Text = arrtmp[6 * i + 5].ToString();
            }
        }



        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RM690B0_QSPI_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else
            {
                //创建保存对话框对象
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "gamma cfg files (*.txt)|*.txt"; //过滤文件类型
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(RM690B0_QSPI_Gamma_SetBox.Text);
                    sw.Close();
                }
            }
        }
        private void 载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "精度配置文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "gamma cfg write";

            if (of.ShowDialog() == DialogResult.OK)
            {
                RM690B0_QSPI_Gamma_SetBox.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    RM690B0_QSPI_Gamma_SetBox.AppendText(str_generate + "\r\n");
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }

        //以下未用
        #region
        //未用

        private void RM690B0_QSPI_Gamma_Load(object sender, EventArgs e)
        {

        }
        private ushort[] gamma_r = new ushort[33];
        private ushort[] gamma_g = new ushort[33];
        private ushort[] gamma_b = new ushort[33];
        private void Rbt_RM690B0_QSPI_P15_CheckedChanged(object sender, EventArgs e)
        {
            List<NumericUpDown> R_Value = new List<NumericUpDown> { RM690B_R0, RM690B_R1, RM690B_R2, RM690B_R3, RM690B_R4, RM690B_R5, RM690B_R6, RM690B_R7, RM690B_R8, RM690B_R9, RM690B_R10, RM690B_R11, RM690B_R12, RM690B_R13 };
            List<NumericUpDown> G_Value = new List<NumericUpDown> { RM690B_G0, RM690B_G1, RM690B_G2, RM690B_G3, RM690B_G4, RM690B_G5, RM690B_G6, RM690B_G7, RM690B_G8, RM690B_G9, RM690B_G10, RM690B_G11, RM690B_G12, RM690B_G13 };
            List<NumericUpDown> B_Value = new List<NumericUpDown> { RM690B_B0, RM690B_B1, RM690B_B2, RM690B_B3, RM690B_B4, RM690B_B5, RM690B_B6, RM690B_B7, RM690B_B8, RM690B_B9, RM690B_B10, RM690B_B11, RM690B_B12, RM690B_B13 };
            string str = "";
            listBoxB_1.Items.Clear();
            listBoxB_2.Items.Clear();
            listBoxG_1.Items.Clear();
            listBoxG_2.Items.Clear();
            listBoxR_1.Items.Clear();
            listBoxR_2.Items.Clear();
            if (rbt_RM690B0_QSPI_P27.Checked)
            {
                gamma_r = GAMMAs.IC690B0_Gamma_r;
                gamma_g = GAMMAs.IC690B0_Gamma_g;
                gamma_b = GAMMAs.IC690B0_Gamma_b;
                str = "__GAMMA_SET(0)\r\n"
                        + "TUNING_SPEC(27, 430, 0.300, 0.315, 2.2)\r\n"
                        + "LXY_SPEC(255, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(239, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(223, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(207, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(191, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(175, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(159, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(143, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(127, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(111, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(95, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(79, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(63, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(55, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(47, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(39, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(35, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(31, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(27, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(23, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(19, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(15, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(11, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(7, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(3, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(1, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(0, 0.001, 0.001, 0.15) \r\n"
                        + "__GAMMA_TUNING                  \r\n"
                        + "SAVE                            \r\n";
                listBoxR_1.Items.AddRange(new string[] { "R0", "R1", "R3", "R7", "R11", "R15", "R19", "R23", "R27", "R31", "R35", "R39", "R47", "R55" });
                listBoxR_2.Items.AddRange(new string[] { "R63", "R79", "R95", "R111", "R127", "R143", "R159", "R175", "R191", "R207", "R223", "R239", "R255" });

                listBoxG_1.Items.AddRange(new string[] { "G0", "G1", "G3", "G7", "G11", "G15", "G19", "G23", "G27", "G31", "G35", "G39", "G47", "G55" });
                listBoxG_2.Items.AddRange(new string[] { "G63", "G79", "G95", "G111", "G127", "G143", "G159", "G175", "G191", "G207", "G223", "G239", "G255" });

                listBoxB_1.Items.AddRange(new string[] { "B0", "B1", "B3", "B7", "B11", "B15", "B19", "B23", "B27", "B31", "B35", "B39", "B47", "B55" });
                listBoxB_2.Items.AddRange(new string[] { "B63", "B79", "B95", "B111", "B127", "B143", "B159", "B175", "B191", "B207", "B223", "B239", "B255" });
                for (int i = 0; i < 14; i++)
                {
                    R_Value[i].Enabled = true;
                    G_Value[i].Enabled = true;
                    B_Value[i].Enabled = true;
                }
            }
            else   //P13
            {
                gamma_r = GAMMAs.IC690B0_Gamma_r_13p;
                gamma_g = GAMMAs.IC690B0_Gamma_g_13p;
                gamma_b = GAMMAs.IC690B0_Gamma_b_13p;

                str = "__GAMMA_SET(0)\r\n"
                        + "TUNING_SPEC(13, 430, 0.300, 0.315, 2.2)\r\n"
                        + "LXY_SPEC(255, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(191, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(159, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(127, 0.001, 0.001, 0.1)\r\n"
                        + "LXY_SPEC(95, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(63, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(31, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(15, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(11, 0.001, 0.001, 0.15)\r\n"
                        + "LXY_SPEC(7, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(3, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(1, 0.001, 0.001, 0.15) \r\n"
                        + "LXY_SPEC(0, 0.001, 0.001, 0.15) \r\n"
                        + "__GAMMA_TUNING                  \r\n"
                        + "SAVE                            \r\n";
                listBoxR_1.Items.AddRange(new string[] { "R0", "R1", "R3", "R7", "R11", "R15", "R31", "R63", "R95", "R127", "R159", "R191", "R255", "XXXX" });
                listBoxR_2.Items.AddRange(new string[] { "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX" });
                listBoxG_1.Items.AddRange(new string[] { "G0", "G1", "G3", "G7", "G11", "G15", "G31", "G63", "G95", "G127", "G159", "G191", "G255", "XXXX" });
                listBoxG_2.Items.AddRange(new string[] { "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX" });
                listBoxB_1.Items.AddRange(new string[] { "B0", "B1", "B3", "B7", "B11", "B15", "B31", "B63", "B95", "B127", "B159", "B191", "B255", "XXXX" });
                listBoxB_2.Items.AddRange(new string[] { "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX", "XXXX" });
                for (int i = 0; i < 14; i++)
                {
                    R_Value[i].Enabled = false;
                    G_Value[i].Enabled = false;
                    B_Value[i].Enabled = false;
                }
            }

            RM690B0_QSPI_Gamma_SetBox.Text = str;
        }

        //private void Load_GammmPic(string filenames)      //双击 载入 BMP图片
        //{
        //    Int32 HACT_True = 0;
        //    Int32 HACT_Skip = 0;
        //    Int32 VACT_True = 0;
        //    Int32 m_txlen = 0;
        //    Int32 j = 0;
        //    FileStream aFile;

        //    //byte[] TemFileContent = new byte[1024 * 100];
        //    byte[] TemFileContent = new byte[512 * 4000 + 100];
        //    System.Int32 ByteReaded = 0x00;

        //    string FileName = Gamma_loading_path.Text + '\\' + filenames + ".bmp";
        //    aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        //    ByteReaded = aFile.Read(TemFileContent, 0, 54);

        //    HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
        //    VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

        //    HACT_Skip = HACT_True;
        //    try
        //    { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
        //        TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
        //        frmHelp.comPort.Write(TemFileContent, 0, 5);
        //    }
        //    catch
        //    {
        //        MessageBox.Show("串口有误，请检查", "错误");
        //        return;
        //    }
        //    //-----------------------------------
        //    try
        //    {
        //        j = 1;
        //        aFile.Seek(54, SeekOrigin.Begin);
        //        while (j <= VACT_True)
        //        {
        //            if (HACT_True % 4 != 0)
        //                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
        //            else
        //                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

        //            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
        //            try
        //            {
        //                frmHelp.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
        //            }
        //            catch
        //            {
        //                MessageBox.Show("串口有误，请检查", "错误");
        //                break;
        //            }
        //            m_txlen += ByteReaded;
        //            j++;
        //        }

        //        TemFileContent[0] = 0x5A;
        //        try
        //        {
        //            frmHelp.comPort.Write(TemFileContent, 0, 1);
        //        }
        //        catch
        //        {
        //            MessageBox.Show("串口有误，请检查", "错误");
        //            return;
        //        }
        //        aFile.Close();
        //    }
        //    catch
        //    {
        //        frmHelp.textBox1.Text += "啥图片都没显示";
        //        frmHelp.textBox1.Select(frmHelp.textBox1.Text.Length, 0);
        //        frmHelp.textBox1.ScrollToCaret();
        //    }
        //}
        #endregion

    }
}



